﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyLib;

namespace WebRole1
{
    public partial class ExplorerBlob : BasicPage
    {

        protected string MyList = "";
        protected string MyInfo = "";
        string urlFormat = @"<a href=""{0}"">{1}</a>";

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!ctx.MyClsStorageAccount.IsAccountValid)
            {
                Response.Redirect("login.aspx");
            }

            if (!IsPostBack)
            {
                mInit();
            }

        }


        private void mInit()
        {
            try
            {
                refreshListContainers();
            }
            catch (Exception ex)
            {
                TraceException(ex);
            }
        }

        private void TraceException(Exception ex)
        {
            MyInfo = MyLib.MyAPI.GetErrorMessage(ex);
        }

        protected void btnRefreshListContainer_Click(object sender, EventArgs e)
        {
            try
            {
                refreshListContainers();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void refreshListContainers()
        {
            listContainers.DataSource = MyLib.MyAPI.ContainerList(ctx);
            listContainers.DataBind();

        }

        protected void btnGetBlobNames_Click(object sender, EventArgs e)
        {
            try
            {
                GetBlobNames();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void GetBlobNames()
        {
            string containerName = listContainers.SelectedValue;

            var blobContainer = MyLib.MyAPI.CreateBlobContainer(containerName);
            var permissions = blobContainer.GetPermissions();
            MyInfo += "Blob Permissions" + "<br/>";
            MyInfo += "Public Acces " + permissions.PublicAccess.ToString() + @"<br/>";
            MyInfo += "SharedAccessPolicies" + permissions.SharedAccessPolicies.ToString() + @"<br/>";

            int i = 0;
            listBlobs.DataSource = MyLib.MyAPI.BlobList(containerName, out i);
            listBlobs.DataBind();

            lblBlobs.Text = "Blobs (" + i.ToString() + ")";

            string url = "";
            foreach (ListItem item in listBlobs.Items)
            {
                url = string.Format(urlFormat, item.Value, item.Value);
                MyList = MyList + @"<br/>" + url;
            }
        }

        protected void btnDeleteAllBlobs_Click(object sender, EventArgs e)
        {
            try
            {
                DeleteAllBlobsFromSelectedContainer();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void DeleteAllBlobsFromSelectedContainer()
        {
            string containerName = listContainers.SelectedValue;
            if (containerName == "")
            {
                return;
            }

            int i = MyLib.MyAPI.DeleteAllBlobs(containerName);

            MyInfo = "Amount of deleted Blobs is " + i.ToString();

            GetBlobNames();
        }

    }
}